#!/usr/bin/env python
# encoding: utf-8
# @author: liusir
# @file: requests_utils.py
# @time: 2021/7/7 8:14 下午

import os
import json
import requests
from common.config import Config

conf_file_path = os.path.join( os.path.abspath(os.path.dirname(__file__)),'..','conf','config.ini' )

class RequestsUtils:
    def __init__(self):
        self.session = requests.session()
        self.hosts = Config(conf_file_path).HOSTS

    def request_by_step(self,test_steps):
        for test_step in test_steps:
            result = self.request( test_step )
            if result['code'] != 0:
                break
        return result


    def request(self,step_info):
        request_type = step_info['请求方式']
        if request_type == "get":
            result = self.__get( step_info )
        elif request_type == "post":
            result = self.__post( step_info )
        else:
            result = {'code':1,'result':'请求方式不支持'}
        return result

    def __get(self,request_info):
        url = 'https://%s%s'%(self.hosts,request_info['请求地址'])
        response = self.session.get( url = url,
                                     params = json.loads(request_info['请求参数(get)']),
                                     headers = json.loads(request_info['请求头部信息']) if request_info['请求头部信息'] else None
                                     )
        result = {
            'code':0,
            'response_code':response.status_code,
            'response_reason':response.reason,
            'response_headers':response.headers,
            'response_body':response.text
        }
        return result

    def __post(self,request_info):
        url = 'https://%s%s'%(self.hosts,request_info['请求地址'])
        response = self.session.post( url = url,
                                     params = json.loads(request_info['请求参数(get)']),
                                     headers = json.loads(request_info['请求头部信息']) if request_info['请求头部信息'] else None,
                                     json = json.loads(request_info['请求参数(post)'])
                                     )
        result = {
            'code':0,
            'response_code':response.status_code,
            'response_reason':response.reason,
            'response_headers':response.headers,
            'response_body':response.text
        }
        return result

if __name__=='__main__':
    request_info = {'测试用例编号': 'api_case_01', '测试用例名称': '获取access_token接口测试', '用例执行': '是', '用例步骤': 'step_01', '接口名称': '获取access_token接口', '请求方式': 'get', '请求头部信息': '', '请求地址': '/cgi-bin/token', '请求参数(get)': '{"grant_type":"client_credential","appid":"wx55614004f367f8ca","secret":"65515b46dd758dfdb09420bb7db2c67f"}', '请求参数(post)': '', '取值方式': '无', '取值代码': '', '取值变量': '', '断言类型': 'none', '期望结果': '"access_token":"(.+?)"'}
    r = RequestsUtils()
    # result = r.request(request_info)
    # print( result )
    request_info = {'测试用例编号': 'api_case_02', '测试用例名称': '创建标签接口测试', '用例执行': '否', '用例步骤': 'step_02', '接口名称': '创建标签接口', '请求方式': 'post', '请求头部信息': '', '请求地址': '/cgi-bin/tags/create', '请求参数(get)': '{"access_token":"46_Zcj3iwiHHILu8djI7kNG4b-Td8PPZYfCFv43l4ui-EvYdGWZ_SEeetNUvq-Qw1BkJoQs-ggeFKixHLlqFeT3LqHmVq6k76IwVymhYRQiEisiI2MDkAPjXf-dXXpK3QEmUcs0iCpJ1AWTIqguURXeAIAKHN"}', '请求参数(post)': '{   "tag" : {     "name" : "P7P8new02" } } ', '取值方式': '无', '取值代码': '', '取值变量': '', '断言类型': 'json_key', '期望结果': 'tag'}
    # result = r.request( request_info  )
    # print( result )
    case_info = [{'测试用例编号': 'api_case_02', '测试用例名称': '创建标签接口测试', '用例执行': '否', '用例步骤': 'step_01', '接口名称': '获取access_token接口', '请求方式': 'get', '请求头部信息': '', '请求地址': '/cgi-bin/token', '请求参数(get)': '{"grant_type":"client_credential","appid":"wx55614004f367f8ca","secret":"65515b46dd758dfdb09420bb7db2c67f"}', '请求参数(post)': '', '取值方式': '正则取值', '取值代码': '"access_token":"(.+?)"', '取值变量': 'token', '断言类型': 'json_key_value', '期望结果': '{"expires_in":7200}'}, {'测试用例编号': 'api_case_02', '测试用例名称': '创建标签接口测试', '用例执行': '否', '用例步骤': 'step_02', '接口名称': '创建标签接口', '请求方式': 'post', '请求头部信息': '', '请求地址': '/cgi-bin/tags/create', '请求参数(get)': '{"access_token":"${token}"}', '请求参数(post)': '{   "tag" : {     "name" : "P5P6new01" } } ', '取值方式': '无', '取值代码': '', '取值变量': '', '断言类型': 'json_key', '期望结果': 'tag'}]
    result = r.request_by_step( case_info )


